// 删除评论
function deleteComments() {
    let ids = new Array();
    $('input[name="checkOption"]:checked').each(function () {
        ids.push($(this).parent("th").parent("tr").find("[name='id']").html());
    })

    if (ids.length == 0 ){
        swal("请至少选择一条评论删除",{
            icon: "warning",
            button: {
                text: "好的",
            },
        })
        return;
    }

    swal({
        title: "你确定要删除吗？",
        text: "你将无法恢复它！",
        icon: "warning",
        buttons: ["我点错了","确认删除"],
        dangerMode: true,
    }).then(flag => {
        if(flag){
            $.ajax({
                type: "post",
                url: "/admin/comment/deleteComments",
                data: {"ids" : ids},
                traditional: true,
                success: function (result) {
                    if(result.resultCode == 200){
                        swal("删除成功",{
                            icon: "success",
                            button: {
                                text: "好的",
                            },
                        }).then(function () {
                            location.reload();
                        })
                    }else {
                        swal(result.message,{
                            icon: "error",
                            button: {
                                text: "好的",
                            },
                        })
                    }
                },
                error: function () {
                    swal("操作失败", {
                        icon: "error",
                        button: {
                            text: "好的"
                        },
                    });
                }
            })
        }
    })
}

// 点击回复触发 -> 让模态框显示
function modalShow() {
    // 统计选择的评论数
    let count = 0;
    $('input[name="checkOption"]:checked').each(function () {
        count++;
    });
    if(count == 0) {
        swal("请至少选择一条评论", {
            icon: "warning",
            button: {
                text: "好的"
            },
        });
        return;
    }

    if(count > 1) {
        swal("一次只能回复一条评论", {
            icon: "warning",
            button: {
                text: "好的"
            },
        });
        return;
    }
    // 让模态框显示
    $("#exampleModal").modal("show");
}

function reply() {
    // 获得选中的评论 id
    let id = $('input[name="checkOption"]:checked').parent("th").parent("tr").find("[name='id']").html();
    // 获得回复内容
    let replyBody = $("#replyBody").val();
    if (replyBody.trim() == "" || replyBody == undefined || replyBody == null) {
        swal("请输入回复内容",{
            icon: "warning",
            button: {
                text: "好的"
            },
        })
        return;
    }
    $.ajax({
        type: "post",
        url: "/admin/reply",
        data: {"id":id,"replyBody":replyBody},
        success: function (result) {
            if (result.resultCode == 200) {
                swal("回复成功",{
                    icon: "success",
                    button: {
                        text: "好的",
                    },
                }).then(function () {
                    location.reload();
                })
            }else {
                swal(result.message,{
                    icon: "error",
                    button: {
                        text: "好的",
                    },
                })
            }
        },
        error: function () {
            swal("操作失败", {
                icon: "error",
                button: {
                    text: "好的"
                },
            });
        }
    })
}